package com.dyh.quickdemozwei;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Consumer;

@Slf4j
public class TestBatchRetry {
    public  static  final List<Integer> list = Lists.newArrayList(
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17
    );



    Random random=new Random();


    public List<Integer> page(int start,int size){
        List<List<Integer>> res = Lists.partition(list,size);
        return res.get(start-1);
    }




    public List<Integer> all(){
        int start = 1;
        int size = 3;

        List<Integer> result = new ArrayList<>();
        for (;;){
            log.info("开始拉取第 {} 页",start);
            List<Integer> part = page(start,size);
            if (CollectionUtils.isEmpty(part))
                break;

            if (random.nextInt(10)>7)
                throw new RuntimeException();

            result.addAll(part);

            log.info("结束拉取第 {} 页",start);


            if (part.size()<size)
                break;
            start++;
        }

        return result;
    }


    public List<Integer> allk(){
        int start = 1;
        int size = 3;


        List<Integer> retryIds = new ArrayList<>();

        List<Integer> result = new ArrayList<>();



        for (;;){
            try {
                log.info("拉取第 {} 页",start);
                List<Integer> part = page(start,size);
                if (CollectionUtils.isEmpty(part))
                    break;

                if (random.nextInt(10)>4)
                    throw new RuntimeException();


                result.addAll(part);

                log.info("拉取第 {} 页 成功",start);


                if (part.size()<size)
                    break;

            }
            catch (Exception ex){
                log.info("第 {} 页 失败",start);
                retryIds.add(start);
            }
            finally {
                start++;
            }
        }


        for (;;){
            boolean allSuccess = true;

            for (int i=0;i<retryIds.size();i++){
                if (retryIds.get(i) == Integer.MIN_VALUE)
                    continue;
                log.info("重试拉取第 {} 页",retryIds.get(i));
                List<Integer> part = page(retryIds.get(i),size);
                if (CollectionUtils.isEmpty(part))
                    break;
                if (random.nextInt(10)>4){
                    allSuccess = false;
                }
                else {
                    retryIds.set(i,Integer.MIN_VALUE);
                    result.addAll(part);
                }
            }
            if (allSuccess)
                break;

        }

        return result;
    }

    @Test
    public void test(){
        List<Integer> allResult = allk();

        log.info("allResult={}", JSON.toJSONString(allResult));
    }
}
